<?php
namespace App\Service\Sms;

use AlibabaCloud\Client\AlibabaCloud;


class AliyunSms implements SMSInterface
{

    private $isOk=false;

    function __construct()
    {


        //客户端初始化，可以放到AppServiceProvider，考虑到可能有不同的阿里云账号，暂缓
        AlibabaCloud::accessKeyClient(
            env('AlibabaAccessKeyID'),
            env('AlibabaSECRET'))->regionId('cn-hangzhou')->asDefaultClient();

//        $this->isOk=true;

    }

    /**
     * Create by Peter
     * 2020/06/29 12:07:30
     * Email:904801074@qq.com
     * @param string $phone
     * @param string $signName
     * @param string $templateCode
     * @param array $templateParam
     * @return mixed|void
     * @throws \AlibabaCloud\Client\Exception\ClientException
     */
    public function sendByTemplate(string $phone, string $signName, string $templateCode, array $templateParam=[])
    {
        // TODO: Implement sendByTemplate() method.

//        if(!$this->isOk) return;

        try {


          $data=  AlibabaCloud::rpc()
                ->product('Dysmsapi')
                ->version('2017-05-25')
                ->action('SendSms')
                ->method('POST')
                ->host('dysmsapi.aliyuncs.com')
                ->options([
                    'query' => [
                        'RegionId' => "cn-hangzhou",
                        'PhoneNumbers' => $phone,
                        'SignName' => $signName,
                        'TemplateCode' => $templateCode,
                        'TemplateParam' => json_encode($templateParam),
                    ]

                ])->timeout(10)->connectTimeout(10)->request();


          $data=json_decode($data,true);

          $code=$data['Code'];

          if(!$code==='OK'){

              \Log::debug($data);

          }



        }catch (\Exception $exception){


            return $exception->getMessage();

        }
    }

    /**
     * 查询模板详情
     * Create by PeterYang
     * 2020/06/29 22:48:21
     * Email:904801074@qq.com
     * @param $templateCode
     * @return mixed|void
     * @throws \AlibabaCloud\Client\Exception\ClientException
     * @throws \AlibabaCloud\Client\Exception\ServerException
     */
    public function querySmsTemplate($templateCode):array
    {
        // TODO: Implement querySmsTemplate() method.

//        if(!$this->isOk) return ['code'=>2,'msg'=>'config fail'];

        $result = AlibabaCloud::rpc()
            ->product('Dysmsapi')
            // ->scheme('https') // https | http
            ->version('2017-05-25')
            ->action('QuerySmsTemplate')
            ->method('POST')
            ->host('dysmsapi.aliyuncs.com')
            ->options([
                'query' => [
                    'RegionId' => "cn-hangzhou",
                    'TemplateCode' => $templateCode,
                ],
            ])
            ->timeout(10)->connectTimeout(10)
            ->request();

        $data=$result->toArray();

//        dd($data);
        $code=$data['Code'];

        if($code!=='OK') return ['code'=>2,'message'=>$data['Message']];


        return [
            'code'=>1,
            'msg'=>'success',
            'data'=>[
                'TemplateName'=>$data['TemplateName'],
                'TemplateContent'=>$data['TemplateContent']
            ]


        ];



    }
}
